import React, {Component} from "react";
import SplashView from "../widget/SplashView";
import {Platform, StyleSheet, Text, View, StatusBar} from "react-native";
import {NavigationActions} from "react-navigation";
import SplashScreen from 'react-native-splash-screen'
import DeviceStorage from '../utils/DeviceStorage'
import define from '../utils/define'
export default class SplashScene extends Component<> {

    static navigationOptions = ({navigation}: any) => ({
            header: null,
        }
    )

    switchPage(){
        DeviceStorage.get(define.token)
            .then(res=>{
                if (res){
                    //重置根视图，闪屏页=>主界面后销毁掉
                    const resetAction = NavigationActions.reset({
                        index: 0,
                        actions: [NavigationActions.navigate({ routeName: 'home'})]})
                    this.props.navigation.dispatch(resetAction)
                }else {
                    //重置根视图，闪屏页=>主界面后销毁掉
                    const resetAction = NavigationActions.reset({
                        index: 0,
                        actions: [NavigationActions.navigate({ routeName: 'LoginViewController',params: { push: false,is_mine:false }})]})
                    this.props.navigation.dispatch(resetAction)
                }
            })
            .catch(err=>{

            })
    }

    render() {
        return (
            <View style={styles.container}>
                <StatusBar
                    backgroundColor='#ff0000'
                    translucent={true}
                    hidden={true}
                    animated={true}
                />
                <SplashView source={require('../../res/img/jk_logo.jpg')} animateEnd={()=>{

                    DeviceStorage.get(define.guide_flag)
                        .then(res=>{
                           if (res){
                               //进入过引导图了
                                this.switchPage()
                           }else {
                              //没进入过引导图
                              //重置根视图，闪屏页=>主界面后销毁掉
                               const resetAction = NavigationActions.reset({
                                   index: 0,
                                   actions: [NavigationActions.navigate({ routeName: 'guideScene'})]})
                                   this.props.navigation.dispatch(resetAction)

                           }
                        })
                        .catch(err=>{

                        })

                }}/>
            </View>
        );
    }

    componentDidMount() {
        SplashScreen.hide();//关闭启动屏幕
    }
}


const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
    },
    instructions: {
        textAlign: 'center',
        color: '#333333',
        marginBottom: 5,
    },
});
